package com.jimo.app.adp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jimo.app.R;
import com.jimo.app.bean.MemTypeBean;
import com.jimo.app.databinding.ItemMemTypeBinding;

import java.util.ArrayList;

public class MemTypeAdapter extends ArrayAdapter<MemTypeBean> {

    private final ArrayList<MemTypeBean> data;
    private final LayoutInflater inflater;

    public MemTypeAdapter(@NonNull Context context, ArrayList<MemTypeBean> data) {
        super(context, R.layout.item_mem_type, data);
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ItemMemTypeBinding binding;
        if (convertView == null) {
            binding = ItemMemTypeBinding.inflate(inflater, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (ItemMemTypeBinding) convertView.getTag();
        }

        MemTypeBean bean = data.get(position);
        binding.text.setText(bean.getName());
        return convertView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ItemMemTypeBinding binding;
        if (convertView == null) {
            binding = ItemMemTypeBinding.inflate(inflater, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (ItemMemTypeBinding) convertView.getTag();
        }

        MemTypeBean bean = data.get(position);
        binding.text.setText(bean.getName());
        return convertView;
    }
}
